
#include <iostream>
#include <conio.h>
using namespace std;

struct object{
    int nr;
    struct object *next;
};


class LinkedList{

    private:
        object *start;

    public:
    LinkedList(){
        start = NULL;
    }

    bool isEmpty(){
        return (start==NULL);
    }

    void push(int nr_){
        object *newObject;
        newObject=(object *)malloc(sizeof(object));
        newObject->nr = nr_;
        newObject->next = start;
        start=newObject;
    }

    int getSize(){
        object *p = start;
        int size = 0;

        if(start!=NULL){
            size++;
            while(p->next!=NULL){
                p = p->next;
                size++;}
        }
        return size;
    }


    void insert(int index, int value){
        if(index==0){
            push(value);
            return;}

        object *previous = NULL;
        object *current = start;

        //Find the position in the list
        for(int i = 0; i<index; i++){
            previous = current;
            current = current->next;}

        //Create new object
        object *newObject;
        newObject=(object *)malloc(sizeof(object));
        newObject->nr = value;

        //Redirect pointers
        previous->next = newObject;
        newObject->next = current;
    }

    int pop(){
        int nr = start->nr;
        object *p = start;
        start = start->next;
        free(p);
        return nr;
    }
};


int main(string args[]){
    LinkedList *ll = new LinkedList();

    for(int i = 0; i<20; i++)
        (*ll).push(i);

    cout << "Size of list is " << (*ll).getSize() << " objects.\n\n";

    while(!(*ll).isEmpty())
        cout << (*ll).pop() << endl;

    delete ll;
    cout << "Deleted!" << endl;


    return (EXIT_SUCCESS);
}


